﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace SurfaceRacing
{
    class Car
    {

        private Vector2 position;
        private Vector2 inertiaDirection = Vector2.Zero;
        private float rotation;
        private float speed;
        private float acceleration;
        private float wheelState;
        private Vector2 tagPos;
        
        private float maxAcc = 0.025f;
        private float maxSpeed = 0.2f;
        private Color colour = Color.White;
        private float maxPedalLength = 100f;
        private bool hasUpdatedThisTurn = false;
        private int inertiaModifierMax = 18; // affects how much the car wants to continue in its current direction

        #region getters/setters


        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }
        public float Speed
        {
            get { return speed; }
            set { speed = value; }
        }
        public float Acceleration
        {
            get { return acceleration; }
            set { acceleration = value; }
        }
        public Color Color
        {
            get { return colour; }
            set { colour = value; }
        }
        public bool HasUpdatedThisTurn
        {
            get { return hasUpdatedThisTurn; }
            set { hasUpdatedThisTurn = value; }
        }
        public float MaxPedalLength
        {
            get { return maxPedalLength; }
            set { maxPedalLength = value; }
        }
        public Vector2 InertiaDirection
        {
            get { return inertiaDirection; }
            set { inertiaDirection = value; }
        }
        public float WheelState
        {
            get { return wheelState; }
            set { wheelState = value; }
        }
        public Vector2 TagPos
        {
            get { return tagPos; }
            set { tagPos = value; }
        }

        #endregion

        public void accelerate(float pedalDistance)
        {
            if (pedalDistance > maxPedalLength)
                pedalDistance = maxPedalLength;
            else if (pedalDistance < -maxPedalLength)
                pedalDistance = -maxPedalLength;

            pedalDistance = pedalDistance / maxPedalLength;


            if ((((speed / maxSpeed) < pedalDistance) && (pedalDistance > 0))||
                (((speed / maxSpeed) > pedalDistance) && (pedalDistance < 0)))
            {
                speed += (pedalDistance * maxAcc);
                if (speed > maxSpeed)
                    speed = maxSpeed;
                else if (speed < -maxSpeed)
                    speed = -maxSpeed;
            }
            else if ((((speed / maxSpeed) > pedalDistance) && (pedalDistance > 0)) ||
                    (((speed / maxSpeed) < pedalDistance) && (pedalDistance < 0)))
            {
                speed -= ((1 - pedalDistance) * maxAcc);
                if (speed > maxSpeed)
                    speed = maxSpeed;
                else if (speed < -maxSpeed)
                    speed = -maxSpeed;
            }
            else if (pedalDistance == 0)
            {
                speed -= speed * maxAcc;
                if (Math.Abs(speed) < 0.0001)
                    speed = 0;
            }
            
            float changeInX, changeInY;
            changeInX = (float)(Math.Sin(rotation) * speed);
            changeInY = -(float)(Math.Cos(rotation) * speed);

            inertiaDirection += new Vector2(changeInX, changeInY);

            if (inertiaDirection.Length() > (Math.Abs(Speed) * inertiaModifierMax * Math.Abs(pedalDistance)))
            {
                inertiaDirection.Normalize();
                inertiaDirection = inertiaDirection * (inertiaModifierMax * Math.Abs(Speed) * Math.Abs(pedalDistance));
            }
        }


        public void move(float friction)
        {

            position += inertiaDirection / friction;
        }


        public Car(Vector2 tagPos)
        {
            position = new Vector2(512, 384);
            speed = 0f;
            rotation = 0f;
            acceleration = 0f;
            this.tagPos = tagPos;
            Random r = new Random();
            byte[] b = new byte[3];
            r.NextBytes(b);
            colour = new Color(b[0],b[1],b[2]);
        }
    }
}
